package cn.laixueit.cart.service.impl;

import cn.laixueit.mapper.*;
import cn.laixueit.cart.pojo.item.*;
import cn.laixueit.cart.pojo.page.PageResult;
import cn.laixueit.cart.service.CategoryService;
import cn.laixueit.cart.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper , Spu> implements SpuService {


    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SpuDetailMapper spuDetailMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageResult<SpuVo> findPage(String key, Boolean saleable, Integer page, Integer rows) {
        PageResult<SpuVo> result = new PageResult<>();

        Page<Spu> p = new Page<>(page, rows);
        QueryWrapper<Spu> warpper = getWarpper(key, saleable);
        IPage<Spu> spuIPage = page(p, warpper);

        List<Spu> spuList = spuIPage.getRecords();
        List<SpuVo> spuVos = new ArrayList<>();
        spuList.forEach(spu -> {
            SpuVo spuVo = new SpuVo();
            BeanUtils.copyProperties(spu,spuVo);

            Brand brand = brandMapper.selectById(spu.getBrandId());
            spuVo.setBname(brand.getName());

            List<String> names = categoryService.categoryNames(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            spuVo.setCname(StringUtils.join(names,"/"));

            spuVos.add(spuVo);
        });


        result.setItems(spuVos);
        result.setTotal(spuIPage.getTotal());
        return result;
    }


    @Transactional
    public void saveGoods(SpuVo spuVo) {

        spuVo.setCreateTime(new Date());
        spuVo.setValid(true);
        spuVo.setSaleable(true);
        spuVo.setLastUpdateTime(new Date());
        save(spuVo);

        SpuDetail spuDetail = spuVo.getSpuDetail();
        spuDetail.setSpuId(spuVo.getId());
        spuDetailMapper.insert(spuDetail);

        List<Sku> skus = spuVo.getSkus();

        for (Sku sku : skus) {
            sku.setCreateTime(new Date());
            sku.setSpuId(spuVo.getId());
            sku.setEnable(true);
            sku.setCreateTime(new Date());
            sku.setLastUpdateTime(new Date());

            skuMapper.insert(sku);


            Stock stock = new Stock();
            stock.setStock(sku.getStock());
            stock.setSkuId(sku.getId());

            stockMapper.insert(stock);
        }
        sendMsg("insert", spuVo.getId());
    }

    @Override
    public List<Sku> skuListBySpuId(Long id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("spu_id",id);
        List<Sku> skuList = skuMapper.selectByMap(map);
        return skuList;
    }

    @Override
    public SpuDetail findDetailBySpuId(Long id) {
        return spuDetailMapper.selectById(id);
    }

    @Override
    public Sku getSkuById(Long id) {
        Sku sku = skuMapper.selectById(id);
        return sku;
    }

    private QueryWrapper<Spu> getWarpper(String key,Boolean saleable){
        QueryWrapper<Spu> wrapper = new QueryWrapper<>();

        if(key!=null && !"".equals(key.trim())){
            wrapper.like("title",key);
        }
        if(saleable!=null){
            wrapper.eq("saleable",saleable);
        }

        return wrapper;
    }

    private void sendMsg(String type,Long id){
        try {
            rabbitTemplate.convertAndSend("item."+type,id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
